/**
 * 2
 *
 * @Author : roryrywang
 * @DateTime : 24-9-7 下午3:55
 */

#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    long long cost[k][k];
    for (int i = 0; i < k; ++i) {
        for (int j = 0; j < k; ++j) {
            cin >> cost[i][j];
        }
    }

    string s;
    cin >> s;

    long long f[n + 1][n + 1];
    for (int len = 0; len <= n; ++len) {
        if (len % 2) continue;
        for (int i = 0, j = i + len; j <= n; ++i, ++j) {
            if (i == j) {
                f[i][j] = 0;
            } else if (j == i + 2) {
                f[i][j] = cost[s[i] - 'a'][s[j - 1] - 'a'];
            } else {
                f[i][j] = 0;
                for (int l = i; l <= j - 2; l++) {
                    long long tmp = cost[s[l] - 'a'][s[l + 1] - 'a'];
                    if ((l - i) % 2) {
                        tmp += cost[s[l - 1] - 'a'][s[l + 2] - 'a'];
                        if (i < l - 1) tmp += f[i][l - 1];
                        if (l + 3 < j) tmp += f[l + 3][j];
                    } else {
                        tmp += f[i][l];
                        tmp += f[l + 2][j];
                    }
                    f[i][j] = max(f[i][j], tmp);
                }
            }
        }
    }

    cout << f[0][n];

    return 0;
}